#ifndef ABSTRACTFITTER_H
#define ABSTRACTFITTER_H

#include "eigenhelper.h"


class AbstractFitter {

public:
    AbstractFitter() : m_x(), m_y() {};

    virtual bool setPoints(const Vecd &x, const Vecd &y) {
        return false;
    }

    virtual bool setPoints(const std::vector<double> &x, const std::vector<double> &y) {
        return setPoints(toEigenMap(x), toEigenMap(y));
    }

    virtual bool setPoints(const QVector<double> &x, const QVector<double> &y) {
        return setPoints(toEigenMap(x), toEigenMap(y));
    }


    virtual double operator()(double x) {
        return 0;
    }

    virtual std::vector<double> operator()(const std::vector<double> &x) {
        auto ret = operator()(toEigenMap(x));
        return toStdVector(ret);
    }

    virtual QVector<double> operator()(const QVector<double> &x) {
        auto ret = operator()(toEigenMap(x));
        return toQVector(ret);
    }

    virtual Vecd operator()(const Vecd &x) {
        return x.unaryExpr([this](double val) { return operator()(val); });
    }


protected:
    Eigen::ArrayXd m_x;
    Eigen::ArrayXd m_y;

    static void bubbleSort(double *x, double *y, size_t n) {
        for (size_t i = 0; i < n; i++) {
            for (size_t j = 0; j < n - 1 - i; j++) {
                if (x[j] > x[j + 1]) {
                    double t1 = x[j];
                    double t2 = y[j];
                    x[j] = x[j + 1];
                    x[j + 1] = t1;
                    y[j] = y[j + 1];
                    y[j + 1] = t2;
                }
            }
        }
    }
};


#endif // ABSTRACTFITTER_H
